import Mock from 'mockjs'
import chaicat from '@/mock/cat.json';
// console.log(chaicat);
let data = Mock.mock({
    'list|100': [{
        id: '@increment(1)',
        name: "@cword(20,30)",
        describe: "@csentence(0,66)",
        "price|80-9999": 1,
        img: "@image('120x90', '#d5d4d4', '#ffffff', 'png', 'IMG')",
        "goumai|0-9999": 1,

    }]
})
let nav = Mock.mock({
    'list|30': [{
        id: '@increment(1)',
        name: "@cword(2,4)",
        'fenlei|1-3': [{ name: "@cword(2,4)", id: '@increment(1)', 'list|5-20': [{ name: "@cword(2,4)", id: '@increment(1)', img: "@image('120x90', '#d5d4d4', '#ffffff', 'png', 'IMG')" }] }]
    }]
})
//根据url获取参数
const get = (url, name) => {
    //获取传递的参数
    const index = url.indexOf('?')
    if (index !== -1) {
        const str = url.substr(index + 1).split('&')
        for (let i = 0; i < str.length; i++) {
            const item = str[i].split('=')
            if (item[0] === name) {
                return item[1]

            }
        }
    }
    return null
}
//获取列表页码接口
//接口写正则 分页接口  params
Mock.mock(/\/chai\/list/, 'get', (options) => {
    //解析传递的参数
    const pageindex = get(options.url, 'pageindex') //第几页
    const pagesize = get(options.url, 'pagesize') //每页几条数据
    // 截取位置的起点
    const start = (pageindex - 1) * pagesize
    //截取位置的终点
    const end = pageindex * pagesize
    //计算总页数
    const total = Math.ceil(data.list.length / pagesize)
    const lists = pageindex > total ? [] : data.list.slice(start, end)
    // console.log('[ lists ] >', lists)
    return {
        message: '获取成功',
        status: 200,
        list: lists,
        listLength: data.list.length
    }
})
//获取分类
Mock.mock('/chai/fenlei', 'get', () => {
    return {
        message: '获取成功',
        status: 200,
        list: nav.list,
        listLength: nav.list.length
    }
})
// console.log('[ nav ] >', nav)
//查看购物车
Mock.mock('/chai/cat/look', 'get', () => {
    // const body = JSON.parse(options.body)
    // const index = data.list.findIndex(item => {
    //     return item.id === body.id
    // })
    // let arr = { ...data.list[index] }
    // arr.num = 1
    // arr.checked = false
    // chaicat.push(arr)
    return {
        message: '看个',
        status: 200,
        list: chaicat,
        total: chaicat.length
    }

})
//获取详情数据
Mock.mock(/\/chai\/xiangqing/, 'get', (options) => {
    const pageindex = get(options.url, 'id') //第几页

    const index = data.list.findIndex(item => {
        return item.id == pageindex
    })
    // console.log('[ index ] >', index)
    let arr = { ...data.list[index] }
    // console.log('[ arr ] >', arr)
    return {
        message: '看个',
        status: 200,
        list: arr,
        total: chaicat.length
    }

})
// 加入购物车
Mock.mock('/chai/cat/add', 'post', (options) => {
    const body = JSON.parse(options.body)
    const index = data.list.findIndex(item => {
        return item.id === body.id
    })
    let arr = { ...data.list[index] }

    arr.num = 1
    arr.checked = false
    let find = chaicat.findIndex(item => {
        return item.id == arr.id
    })
    console.log('[ find ] >', find)
    if (find == -1) {
        chaicat.push(arr)
        return {
            message: '添加成功',
            status: 200,
            list: chaicat,
            total: chaicat.length
        }
    }
    chaicat[find].num = chaicat[find].num + 1
    return {
        message: '购物车已有这件商品',
        status: 200,
        list: chaicat,
        total: chaicat.length
    }
    // const body = JSON.parse(options.body)
    // data.list.push(Mock.mock({
    //     id: '@increment(1)',
    //     name: body.title,
    //     idCard: "@id()",
    //     address: body.content,
    //     img_url: "@image('120x90', '#FF83FA', '#FCFCFC', 'png', '哈哈哈')"

    // }))
    // return {
    //     message: '添加成功',
    //     status: 200,
    //     list: data.list,
    //     total: data.list.length
    // }
})
//删除功能
Mock.mock('/chai/cat/remove', 'post', (options) => {
    const body = JSON.parse(options.body)
    const index = chaicat.findIndex(item => {
        return item.id == body.id
    })
    if (index == -1) {
        return {
            message: '删除失败',
            status: 200,
            list: chaicat,
            total: chaicat.length
        }
    }
    chaicat.splice(index, 1)
    return {
        message: '删除成功',
        status: 200,
        list: chaicat,
        total: chaicat.length
    }
})
console.log(6666666)
//添加数量
Mock.mock('/chai/cat/addnum', 'post', (options) => {
    const body = JSON.parse(options.body)
    const index = chaicat.findIndex(item => {
        return item.id == body.id
    })
    if (index == -1) {
        return {
            message: '添加失败',
            status: 200,
            list: chaicat.list,
            total: chaicat.length
        }
    }
    chaicat[index].num = chaicat[index].num + 1
    return {
        message: '添加成功',
        status: 200,
        list: chaicat,
        total: chaicat.length
    }
})
//减少数量
Mock.mock('/chai/cat/jiannum', 'post', (options) => {
    const body = JSON.parse(options.body)
    const index = chaicat.findIndex(item => {
        return item.id == body.id
    })
    if (index == -1) {
        return {
            message: '减少失败',
            status: 200,
            list: chaicat.list,
            total: chaicat.length
        }
    }
    chaicat[index].num = chaicat[index].num - 1
    return {
        message: '减少成功',
        status: 200,
        list: chaicat,
        total: chaicat.length
    }
})
//改变check
Mock.mock('/chai/cat/check', 'post', (options) => {
    const body = JSON.parse(options.body)
    const index = chaicat.findIndex(item => {
        return item.id == body.id
    })
    if (index == -1) {
        return {
            message: '失败',
            status: 200,
            list: chaicat.list,
            total: chaicat.length
        }
    }
    chaicat[index].check = !chaicat[index].check
    return {
        message: 'ok',
        status: 200,
        list: chaicat,
        total: chaicat.length
    }
})
//购物车全选
Mock.mock('/chai/cat/checkall', 'post', (options) => {
    const body = JSON.parse(options.body)
    // console.log(body)
    chaicat.forEach(item => {
        item.check = body.check
    })
    // if (index == -1) {
    //     return {
    //         message: '失败',
    //         status: 200,
    //         list: chaicat.list,
    //         total: chaicat.length
    //     }
    // }
    // chaicat[index].check = !chaicat[index].check
    return {
        message: '修改成功',
        status: 200,
        list: chaicat,
        total: chaicat.length
    }
})
